package resource.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import resource.InventoryManager;

/**
 * Parser SAX
 * @author Kent
 *
 */
public class CombineHandler extends DefaultHandler {

	private InventoryManager im;
	
	public CombineHandler(InventoryManager i){
		im = i;
	}

	public void characters(char[] buffer, int start, int length) {
	}

	public void startElement(String uri, String name, String qName, Attributes attributes){
		if (qName.equals("combine")){
				im.getItem(attributes.getValue("name")).AddPossibilitie(attributes.getValue("combinedWith"), im.getItem(attributes.getValue("result")), attributes.getValue("consumed"));
		};
	}

	// When the parser encounters the end of an element, it calls this method
	public void endElement(String uri, String name, String qName){
		
	}

	/** This method is called when warnings occur */
	public void warning(SAXParseException exception) {
		System.err.println("WARNING: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/** This method is called when errors occur */
	public void error(SAXParseException exception) {
		System.err.println("ERROR: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/** This method is called when non-recoverable errors occur. */
	public void fatalError(SAXParseException exception) throws SAXException {
		System.err.println("FATAL: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
		throw (exception);
	}
}